/*
 *  RegExpUtil.java
 * 
 *  Copyright (C) 2012-2013 Sylvain Lamprier, Tewfik Ziaidi, Lom Messan Hillah and Nicolas Baskiotis
 * 
 *  This file is part of CARE.
 * 
 *   CARE is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   CARE is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with CARE.  If not, see <http://www.gnu.org/licenses/>.
 */

package regexp;

import java.util.List;

//import regexp.fromLTS.EmptySet;
//import regexp.fromLTS.Epsilon;
import regexp.fromFSA.Variable;
 
/**
 * 
 * 
 * @author Lom Messan Hillah
 * @author Tewfik Ziaidi
 * @author Sylvain Lamprier
 *
 * Defines tools for regular expressions  
 *
 */

public class RegexpUtil {
	public static boolean contains(Regexp container, Regexp contained) {
		if (container.equals(contained)) {
			return true;
		} else {
			if (container instanceof Alternation) {
				for (Regexp exp : ((Alternation)container).getExp()) {
					if (contains(exp, contained)) {
						return true;
					}
				}				
			} else if (container instanceof Concatenation) {
					for (Regexp exp : ((Concatenation)container).getExp()) {
						if (contains(exp, contained)) {
							return true;
						}
					}				
			} else if (container instanceof KleeneStar) {
				return contains(((KleeneStar)container).getExp(), contained);
			} 			
		}		
		return false;
	}
	
	public static String reg2String(Regexp reg) {
		String s = "";
		
		if(reg instanceof Alternation) {
			s += " { ";
			List<Regexp> list = ((Alternation) reg).getExp();
			for(Regexp re : list) {
				s += (reg2String(re)+" + ");
			}
			s += " } ";
			
		} else if(reg instanceof Concatenation) {
			s += " { ";
			List<Regexp> list = ((Concatenation) reg).getExp();
			for(Regexp re : list) {
				s += reg2String(re);
			}
			s += " } ";
			
		} else if(reg instanceof KleeneStar) {
			s += " { ";
			Regexp re = ((KleeneStar) reg).getExp();
			s += reg2String(re);
			s += " }* ";
			
		}/* else if(reg instanceof Epsilon) {
			s += " epsilon ";
			
		} else if(reg instanceof EmptySet) {
			s +="{}";
			
		}*/
		else if(reg instanceof Literal) {
			s += ((Literal) reg).getValue();
			
		} else if(reg instanceof Variable) {
			s += ((Variable) reg).getName();
			
		} else {
			System.out.println("Wrong reg to print :RegexpUtil.java");
		}
		
		return s;
	}
}
